using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MongoScienceDAL.Core.Tests
{
    [TestClass]
    public class ScienceVersionedFileRespositoryTests : MongoGridFsTestBase
    {
        [TestMethod]
        public void UploadCorrectlyUploadsFile()
        {
            CleanupAllFiles();
            
            var repo = GetVersionedFileRepository();

            Assert.AreEqual(0, GetGridFSReference().Files.Count());

            var versionedFile = repo.Upload(GetStream("test value 1"), "test123.exe", "testuser", "1.0");

            Assert.AreEqual(1, GetGridFSReference().Files.Count());
        }

        [TestMethod]
        public void UploadCorrectlyFilesOutReturnInfomation()
        {
            CleanupAllFiles();

            const string testValue = "test value 1";
            const string filename = "test123.exe";
            const string user = "testuser";
            const string version = "1.0";

            var repo = GetVersionedFileRepository();

            Assert.AreEqual(0, GetGridFSReference().Files.Count());

            var stream = GetStream(testValue);
            var versionedFile = repo.Upload(stream, filename, user, version);

            Assert.AreEqual(1, GetGridFSReference().Files.Count());

            //check size
            Assert.AreEqual(stream.Length, versionedFile.Size);

            //check filename
            Assert.AreEqual(filename, versionedFile.Filename);

            //check date
            Assert.IsTrue(DateTime.Now.ToUnixEpochTime() - versionedFile.UploadedOn.ToUnixEpochTime() < 10000); //10 sec

            //check fileid not null
            Assert.IsNotNull(versionedFile.FileId);

            //check uploadedby not null
            Assert.AreEqual(user,versionedFile.UploadedBy);

            //check version is correct
            Assert.AreEqual(version, versionedFile.Version);
        }

        [TestMethod]
        public void DownloadCorrectlyDownloadsTheFileAsAStream()
        {
            const string testValue = "test value 1";

            CleanupAllFiles();

            var repo = GetVersionedFileRepository();

            var versionedFile = repo.Upload(GetStream(testValue), "test123.exe", "testuser", "1.0");

            var stream = repo.Download(versionedFile);

            var resultingValue = ReadStream(stream);

            Assert.AreEqual(testValue, resultingValue);
        }

        [TestMethod]
        public void DeleteCorrectlyDeletesFile()
        {
            CleanupAllFiles();

            var repo = GetVersionedFileRepository();
            Assert.AreEqual(0, GetGridFSReference().Files.Count());

            var versionedFile = repo.Upload(GetStream("test value 1"), "test123.exe", "testuser", "1.0");

            Assert.AreEqual(1, GetGridFSReference().Files.Count());

            repo.Delete(versionedFile);

            Assert.AreEqual(0, GetGridFSReference().Files.Count());
        }
    }
}